//
//  kindManagerViewController.m
//  MoneyLog
//
//  Created by Max on 11-5-8.
//  Copyright 2011年 wicresoft. All rights reserved.
//

#import "KindManagerViewController.h"
#import "KindListSelectedViewController.h"
#import "KindWrapper.h"

extern NSString *kSectionTitleKey;
extern NSString *kRowItemKey;

@implementation KindManagerViewController
@synthesize segmentControl;
@synthesize kindController;
@synthesize kind;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [segmentControl release];
    [kindController release];
    [kind release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"选择类别";
    
    UIBarButtonItem* addItem = [[UIBarButtonItem alloc] 
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                target:self
                                action:@selector(addAction:)];
	self.navigationItem.rightBarButtonItem = addItem;
	[addItem release];
    
    kindController=[[KindListSelectedViewController alloc] initWithNibName:@"KindListSelectedViewController" bundle:nil];
    kindController.myViewDelegate = self;
    kindController.titleString=@"选择类别";
    kindController.kind = self.kind;
    
    NSLog(@"kind = %@",self.kind);

    //[segmentedControl setSelectedSegmentIndex:0];
    NSMutableArray *dataArray =[NSMutableArray array];
    
    NSMutableArray *array = [g_Database getMarkKind];
    NSDictionary *markDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"收藏",kSectionTitleKey,
                                    array,kRowItemKey,nil];
    
    [dataArray addObject:markDictionary];
    [array release];
    
    array = [g_Database getCommonKind:7];
    kindController.view.tag = kKindViewCommon;
    
    NSDictionary *commonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"常用",kSectionTitleKey,
                                      array,kRowItemKey,nil];
    
    
    [dataArray addObject:commonDictionary];
    [kindController reloadData:dataArray];
    [array release];

    [self.view insertSubview:kindController.view atIndex:0];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - * mark action

-(IBAction) segmentAction: (id) sender
{
    NSMutableArray *array=nil;
    NSMutableArray *dataArray =[NSMutableArray array];
    
	switch ([sender selectedSegmentIndex]) {
		case 0:
        {
            array = [g_Database getMarkKind];
            NSDictionary *markDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              @"收藏",kSectionTitleKey,
                                              array,kRowItemKey,nil];
            
            [dataArray addObject:markDictionary];
            [array release];
            
            array = [g_Database getCommonKind:7];
            kindController.view.tag = kKindViewCommon;
            
            NSDictionary *commonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"常用",kSectionTitleKey,
                                        array,kRowItemKey,nil];
            
           
            [dataArray addObject:commonDictionary];
            [array release];
			break;
        }
		case 1:
        {
            array = [g_Database getKind:@" type='收入' AND parentId=0"];
            kindController.view.tag = kKindViewIncome; 
            
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"",kSectionTitleKey,
                                        array,kRowItemKey,nil];
        
            [dataArray addObject:dictionary];
            [array release];
            break;
        }
		case 2:
        {
            array = [g_Database getKind:@" type='支出' AND parentId=0"];
            kindController.view.tag = kKindViewExpend;
            
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"",kSectionTitleKey,
                                        array,kRowItemKey,nil];
            
            [dataArray addObject:dictionary];
            [array release];
			break;
        }
		default:
			break;
	}    
    
    kindController.kind = self.kind;
    [kindController reloadData:dataArray];
}

- (void)addAction:(id)sender
{    
    
}

#pragma mark - * MyView delegate

-(void) myView:(UIView *)myView id:(id)sender{
    self.kind = sender;
    [self.navigationController popViewControllerAnimated:YES];
}


@end
